package com.company.eos.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.eos.common.util.PageUtils;
import com.company.eos.mapper.AuditFlowMapper;
import com.company.eos.mapper.AuditQuitMapper;
import com.company.eos.domain.*;
import com.company.eos.service.AuditDetailService;
import com.company.eos.service.AuditFlowService;
import com.company.eos.service.AuditQuitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author Real
 * @description 针对表【tb_audit_quit(OA离职申请表)】的数据库操作Service实现
 * @createDate 2022-04-27 20:31:57
 */
@Slf4j
@Service
public class AuditQuitServiceImpl extends ServiceImpl<AuditQuitMapper, AuditQuit>
        implements AuditQuitService {

    @Resource
    private AuditQuitMapper auditQuitMapper;
    @Resource
    private AuditFlowMapper auditFlowMapper;
    @Autowired
    private AuditFlowService auditFlowService;
    @Autowired
    private AuditDetailService auditDetailService;

    @Override
    public PageUtils searchQuitByPage(HashMap param) {
        ArrayList<HashMap> list = auditQuitMapper.searchQuitByPage(param);
        long count = auditQuitMapper.searchQuitCount(param);
        int start = (Integer) param.get("start");
        int length = (Integer) param.get("length");
        return new PageUtils(list, count, start, length);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(AuditQuit auditQuit) {
        // 还需要插入对应的审批流表和审批详情表
        int flowId = auditFlowService.insertQuitAudit(auditQuit);
        AuditFlow flow = auditFlowMapper.selectById(flowId);
        // 审核详情表
        auditQuit.setFlowId(flowId);
        auditQuitMapper.insert(auditQuit);
        return auditDetailService.insertAuditQuit(auditQuit, flow);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(AuditLeave auditLeave) {
        // 还需要插入对应的审批流表和审批详情表
        int flowId = auditFlowService.insertLeaveAudit(auditLeave);
        AuditFlow flow = auditFlowMapper.selectById(flowId);
        // 审核详情表，请假表数据已经添加，不需要重复添加
        return auditDetailService.insertAuditLeave(auditLeave, flow);
    }

    @Override
    public int isExistQuit(int userId) {
        log.info("查询是否存在离职申请记录，用户ID：{}", userId);
        Long aLong = auditQuitMapper.selectCount(Wrappers.lambdaQuery(AuditQuit.class)
                .eq(AuditQuit::getApplyUserId, userId)
                .eq(AuditQuit::getStatus, 1));
        return aLong.intValue();
    }

    @Override
    public int delete(Integer[] ids) {
        // 根据ID列表删除离职记录
        List<Integer> list = new ArrayList<>();
        for (Integer id : ids) {
            list.add(id);
            // 还需要删除相关的审批记录
            AuditQuit auditQuit = auditQuitMapper.selectOne(Wrappers.lambdaQuery(AuditQuit.class)
                    .eq(AuditQuit::getId, id));
            auditDetailService.deleteAudit(auditQuit.getFlowId());
        }
        return auditQuitMapper.deleteBatchIds(list);
    }
}




